﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AirlineProject.Models;

namespace AirlineProject.Models
{
    public class AccountModels
    {
        private DataClassesProjectAirlineDataContext dc = new DataClassesProjectAirlineDataContext();
        public List<Destination> ListDes()
        {
            return this.dc.Destinations.ToList();
        }
        public List<Orgin> ListOrg()
        {
            return this.dc.Orgins.ToList();
        }
        public List<Flight> SearchFlight(int from, int to, DateTime departure_date)
        {
            return this.dc.Flights.Where(tk => tk.origin_id == from & to == tk.destination_id & tk.departure_date == departure_date).ToList();
        }
        public List<Flight> ListFlight(int from)
        {
            return this.dc.Flights.Where(f => f.origin_id == from).ToList();
        }
        public List<Destination> ListCitySearch(int place_parentid)
        {
            return this.dc.Destinations.Where(b => b.place_parentid == place_parentid).ToList();
        }
        public List<User> ListUser()
        {
            return this.dc.Users.ToList();
        }
        public User LoginAdmin(string user_name, string password)
        {
            User result = new User();
            try
            {
                result = this.dc.Users.Single(ur => ur.user_name.Equals(user_name) & ur.password.Equals(password));
            }
            catch
            {
                result = null; 
            }
            return result;
        }
        public User Information(string user_name)
        {
            return this.dc.Users.Single(ur => ur.user_name.Equals(user_name));
        }
        public bool DeleteUser(int id)
        {
            bool kq = true;
            try
            {
                User ur = this.dc.Users.Single(usr => usr.user_id.Equals(Convert.ToInt32(id)));
                this.dc.Users.DeleteOnSubmit(ur);
                this.dc.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
            return kq;
        }
        public bool Login(string user_name, string password)
        {
            return this.dc.Users.Where(ur => ur.user_name.Equals(user_name) & ur.password.Equals(password)).Count()==1;
        }
        public bool AddUser(string user_name, string password, string first_name, string last_name, string address, string phone_number, string email, int gender, DateTime DOB, int rights)
        {
            bool kq = true;
            try
            {
                User ur = new User();
                ur.user_name = user_name;
                ur.password = password;
                ur.first_name = first_name;
                ur.last_name = last_name;
                ur.address = address;
                ur.phone_number = phone_number;
                ur.email = email;
                ur.gender = gender;
                ur.DOB = DOB;
                ur.rights = rights;
                this.dc.Users.InsertOnSubmit(ur);
                this.dc.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
            return kq;
        }
    }
}